package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootScinfo;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.JsonReturn;
import org.footballmanager.pandafootball.system.domain.enums.JNFL;
import org.footballmanager.pandafootball.system.form.impl.FootScinfoSearchForm;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootScinfoService;
import org.footballmanager.pandafootball.system.service.FootTableTitleService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.impl.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SessionAttributes("footScinfoSearchForm")
@RequestMapping("/hy/footScinfo")
public class HyScinfoController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footScinfo";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootScinfoService footScinfoService;
    @Autowired
    private FootPersonService footPersonService;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootTableTitleService footTableTitleService;
    @Autowired
    private RedisService redisService;

    /**
     * 初始化
     *
     * @param map                  参数
     * @param footScinfoSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, HttpServletRequest request, @ModelAttribute FootScinfoSearchForm footScinfoSearchForm) {
        map.addAttribute("qyList", footPersonService.getall());
        map.addAttribute("djList", redisService.get("listdj"));
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        map.addAttribute("head", footTableTitleService.findByTableName(Constants.PERSONTABLE));
        FootScinfoSearchForm newFootScinfoSearchForm = new FootScinfoSearchForm();
        if (footScinfoSearchForm != null) {
            newFootScinfoSearchForm = footScinfoSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootScinfoSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, HttpServletRequest request,
                         @ModelAttribute FootScinfoSearchForm footScinfoSearchForm) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        footScinfoSearchForm.setUserId(user.getId());
        footScinfoService.search(ENTITYNAME, map, footScinfoSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn delete(@RequestParam Long qyId, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        FootScinfo fs = footScinfoService.findByUserIdAndQyid(userId, qyId);
        if (fs != null) {
            if (null != fs.getId()) {
                footScinfoService.delete(fs.getId(), request);
            }
            //判断是否有人收藏,并更改兴趣状态
            FootPerson fp = footPersonService.get(qyId);
            List<FootScinfo> scList = footScinfoService.findByQyid(qyId);
            if (scList != null && !scList.isEmpty()) {
                fp.setXqzt(1);
            } else {
                fp.setXqzt(0);
            }
            footPersonService.updateNotNull(fp, request);
            return new JsonReturn(Constants.OK);
        } else {
            return new JsonReturn(Constants.ERROR);
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn save(@RequestParam Long qyId, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        if (userId == null || qyId == null) {
            return new JsonReturn(Constants.ERROR);
        }
        FootScinfo fs = new FootScinfo();
        fs.setQyId(qyId);
        FootPerson fp = footPersonService.get(qyId);
        if (fp != null) {
            fp.setXqzt(1);
            footPersonService.updateNotNull(fp, request);
        }
        fs.setUserId(userId);
        footScinfoService.save(fs, request);
        //更改兴趣状态
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/isSc", method = RequestMethod.POST)
    @ResponseBody
    public Boolean isSc(@RequestParam Long qyId, HttpServletRequest request) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        return footScinfoService.isSc(user.getId(), qyId);
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootScinfoSearchForm footScinfoSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footScinfoSearchForm);
        map.addAttribute("basicSkills", footTableTitleService.findByTableNameAndJnfl(Constants.PERSONTABLE, JNFL.BASIC));
        map.addAttribute("linkageSkills", footTableTitleService.findByTableNameAndJnfl(Constants.PERSONTABLE, JNFL.LINKAGE));
        map.addAttribute("footPerson", footPersonService.get(id));
        return VIEWPAGE;
    }
}
